#include "KmyAlgorithm.h"
#include "ctype.h"

// 来源： http://blog.csdn.net/glp_hit/article/details/8112675
vector<string>* split(const string& src, string separate_character)
{
    vector<string>* strs = new vector<string>(0);
    int separate_characterLen = separate_character.size();//分割字符串长度，这样就可以支持多字 符串的分隔符
    int last_position = 0, index = -1;
    while (-1 != (index = src.find(separate_character, last_position)))
    {
		string s = src.substr(last_position, index - last_position);
        strs->push_back(s);
        last_position = index + separate_characterLen;
    }
    string lastString = src.substr(last_position);
    if (!lastString.empty())
    strs->push_back(lastString);
    return strs;
}

void string_toupper(string &str)
{
    int i, j;

    j = str.length();

    for (i = 0; i < j; i++)
        str[i] = toupper((int)str[i]);
}

bool isStrEqual(char* str1, char *str2){
    int p = 0;
    while (str1[p]==str2[p]){
        p++;
        if (str1[p]==0){
            return true;
        }
    }
    return false;
}

string intToString(int i, const int base){
    char a[MAX_INTEGER_LENGTH];
    int p = 0;
    int i2 = 1;
    while(i>i2*base){
        i2*=base;
    }
    while (i2>=1){
        int i3 = i/i2;
        if (i3>9){
            a[p++] = i3-10+'A';
        }else{
            a[p++] = i3+'0';
        }
        i = i-i3*i2;
        i2/=base;
    }
    a[p]=0;
    string str(a);
    return str;
}

string floatToString(float f){
    int i = (int)f;
    int i2 = 1;
    float f2 = f-i;
    char a[MAX_FLOAT_LENGTH];
    int p = 0;
    while(i>i2*10){
        i2*=10;
    }
    while (i2>=1){
        int i3 = i/i2;
        a[p++] = i3+'0';
        i = i-i3*i2;
        i2/=10;
    }
    a[p++] = '.';
    while(p<MAX_FLOAT_LENGTH-1 && f2>0){
        i = (int)(f2*10);
        a[p++] = i+'0';
        f2 = f2*10-i;
    }
    a[p] = 0;
    string str(a);
    return str;
}

int stringToInt(const string &s){
    const char *p = s.c_str();
    int base = 10;
    if (*p == '0'){
        if (*(p+1) == 'x'){
            base = 16;
            p+=2;
        }else{
            base = 8;
            p+=1;
        }
    }
    int r = 0;
    if (base==16){
        while (*p!=0){
            if (*p<'0' || (*p>'9'&&*p<'A') || (*p>'F'&&*p<'a') || (*p>'f')){
                return r;
            }
            r*=base;
            if (*p>'9'){
                if (*p>'Z'){
                    r+=*p-'a'+10;
                }else{
                    r+=*p-'A'+10;
                }
            }else{
                r+=*p-'0';
            }
            p++;
        }
    }else if (base == 8){
        while (*p!=0){
            if (*p<'0' || *p>'7'){
                return r;
            }
            r*=base;
            r+=*p-'0';
            p++;
        }
    }else{
        while (*p!=0){
            if (*p<'0' || *p>'9'){
                return r;
            }
            r*=10;
            r+=*p-'0';
            p++;
        }
    }
    return r;
}

float stringToFloat(const string &s){
    const char *p = s.c_str();
    float r = 0;
    int b = 1;
    bool is_getpoint = false;
    while (*p!=0){
        if (is_getpoint){
            if (*p<'0' || *p>'9'){
                r = r/b;
                return r;
            }
            r*=10;
            r+=*p-'0';
            b*=10;
        }else{
            if (*p == '.'){
                is_getpoint = true;
            }else{
                if (*p<'0' || *p>'9'){
                    r = r/b;
                    return r;
                }
                r*=10;
                r+=*p-'0';
            }
        }
        p++;
    }
    return r/b;
}

int getStringType(const string &s){
    bool is_getpoint = false;
    const char *p = s.c_str();

    if (*p=='0'&&*(p+1)=='x'){
        p+=2;
        while (*p!=0){
            if (*p<'0' || (*p>'9'&&*p<'A') || (*p>'F'&&*p<'a') || (*p>'f')){
                return 0; // 匹配失败的16进制当作字符串处理
            }
            p++;
        }
        return 1;
    }

    while (*p!=0){
        if (*p<'0'||*p>'9'){
            if (is_getpoint){
                return 0; // 匹配失败的float当作字符串处理
            }else if (*p == '.'){
                is_getpoint = true;
            }else{
                return 0;   //出现其他字符当作字符串处理
            }
        }
        p++;
    }
    if (is_getpoint){
        return 2;   // 小数点是为float
    }else{
        return 1;   // 没有小数点时为int
    }
}

int appendUcs2String(St_Ucs2String *str1, St_Ucs2String *str2){
    if (str2->size==0){
        return str1->size;
    }
    int nsize = str1->size+str2->size-1;
    u16* dbuff = new u16[nsize];
    for (int i=0; i<str1->size-1; i++){
        dbuff[i]=str1->buff[i];
    }
    for (int i=0; i<str2->size; i++){
        dbuff[str1->size-1+i]=str2->buff[i];
    }
    delete str1->buff;
    str1->buff = dbuff;
    str1->size = nsize;
    return nsize;
}
